package com.wsoft.form.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.wsoft.core.exception.Asserts;
import com.wsoft.entity.BaseEntity;
import com.wsoft.form.dto.WfFormVirtualColumnReturnDTO;
import com.wsoft.form.entity.WfFormColumnEntity;
import com.wsoft.form.entity.WfFormTableEntity;
import com.wsoft.form.entity.WfFormTableRelationEntity;
import com.wsoft.form.entity.WfFormVirtualColumnEntity;
import com.wsoft.form.enums.FormEnum;
import com.wsoft.form.manager.WfFormColumnManager;
import com.wsoft.form.manager.WfFormTableManager;
import com.wsoft.form.manager.WfFormTableRelationManager;
import com.wsoft.form.manager.WfFormVirtualColumnManager;
import com.wsoft.form.query.virtual.column.VirtualColumnBaseQuery;
import com.wsoft.form.query.virtual.column.VirtualColumnInsertQuery;
import com.wsoft.form.query.virtual.column.VirtualColumnUpdateQuery;
import com.wsoft.form.service.IWfFormVirtualColumnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 自定义表单-虚拟字段表 服务实现类
 * </p>
 *
 * @author Z001
 * @since 2024年08月29日 09:11:06
 */
@Service
public class WfFormVirtualColumnServiceImpl  implements IWfFormVirtualColumnService {

    @Resource
    private WfFormVirtualColumnManager virtualColumnManager;
    @Resource
    private WfFormColumnManager columnManager;
    @Resource
    private WfFormTableRelationManager relationManager;
    @Resource
    private WfFormTableManager tableManager;

    /**
     * 新增虚拟字段
     *
     * @return
     */
    @Override
    public boolean insertVirtualColumn(VirtualColumnInsertQuery query) {
        WfFormVirtualColumnEntity entity = Convert.convert(WfFormVirtualColumnEntity.class, query);
        //校验虚拟字段名
        checkVirtualColumn(query);
        return virtualColumnManager.save(entity);
    }

    /**
     * 修改虚拟字段
     *
     * @return
     */
    @Override
    public boolean updateVirtualColumn(VirtualColumnUpdateQuery query) {
        WfFormVirtualColumnEntity entity = Convert.convert(WfFormVirtualColumnEntity.class, query);
        //校验虚拟字段名
        checkVirtualColumn(query);
        return virtualColumnManager.updateById(entity);
    }

    /**
     * 获取 表中 所有虚拟字段
     *
     */
    @Override
    public List<WfFormVirtualColumnEntity> listVirtualColumn(Long id) {
        return virtualColumnManager.getByTableId(id);
    }

    /**
     * 删除虚拟字段
     *
     * @return
     */
    @Override
    public boolean delVirtualColumn(Long virtualColumnId) {

        return virtualColumnManager.removeById(virtualColumnId);
    }

    /**
     * 获取虚拟字段详情 ==> 并返回可选择的表信息和字段信息
     *
     * @return
     */
    @Override
    public WfFormVirtualColumnReturnDTO virtualColumnInfo(Long virtualColumnId) {

        WfFormVirtualColumnEntity columnEntity = virtualColumnManager.getById(virtualColumnId);
        if(null == columnEntity){
            return null;
        }
        WfFormVirtualColumnReturnDTO returnDto = Convert.convert(WfFormVirtualColumnReturnDTO.class, columnEntity);

        //查询form 下 非主表的所有表 ==> 这些表是可以用于绑定虚拟字段的
        List<WfFormTableRelationEntity> masterRelation = relationManager.getMasterTable(columnEntity.getFormId(), FormEnum.ZERO.getValue());
        List<Long> collect = masterRelation.stream().map(WfFormTableRelationEntity::getTableId).collect(Collectors.toList());
        List<WfFormTableEntity> tableList = tableManager.listByIds(collect);
        if (CollUtil.isEmpty(tableList)) {
            return returnDto;
        }
        List<Long> tableIdList = tableList.stream().map(BaseEntity::getId).collect(Collectors.toList());

        //获取这些非主表的 所有字段
        List<WfFormColumnEntity> columnList = columnManager.getColumnList(tableIdList);

        //返回表信息和字段信息,用于前端回显和选择表和字段
        returnDto.setTableList(tableList);
        returnDto.setColumnList(columnList);
        returnDto.setAggregationTableName(tableManager.getTableInfo(returnDto.getAggregationTableId()).getTableName());
        returnDto.setAggregationColumnName(columnManager.getById(returnDto.getAggregationColumnId()).getName());
        return returnDto;
    }

    /**
     * 根据id获取
     * @param id
     * @return
     */
    @Override
    public WfFormVirtualColumnEntity get(Long id) {
        return virtualColumnManager.getById(id);
    }


    //############################### 私有函数 ###############################


    /**
     * 校验虚拟字段是否已存在
     *
     */
    private <T extends VirtualColumnBaseQuery> void checkVirtualColumn(T query){
        WfFormVirtualColumnEntity entity = virtualColumnManager.getByColumnName(query.getTableId(), query.getColumnName());

        if(query instanceof VirtualColumnInsertQuery && null != entity){
            //虚拟字段已存在
            Asserts.fail("[wform.virtual.column.exist]");
        }
        if(query instanceof VirtualColumnUpdateQuery && null != entity && !((VirtualColumnUpdateQuery) query).getId().equals(entity.getId())){
            //虚拟字段已存在
            Asserts.fail("[wform.virtual.column.exist]");
        }
    }
}
